<?php
$_catid = $_GET['catid'] = empty($_GET['catid']) ? '' : intval($_GET['catid']);
$orderby = get_major_key(array('rank' => 'DESC', 'id' => 'DESC'));


$newscats = cache_read('news_categorys.php');
if($_GET['catid'] > 0){
	if(isset($newscats['category_list'][$_GET['catid']]['subcats_list'])){
		$subcats = $newscats['category_list'][$_GET['catid']]['subcats_list'];
		// $subcats[] = $_GET['catid'];
		$subcats = implode(',', $subcats);
		$_GET['kept'] = " AND `catid` IN ({$subcats})";
		unset($_GET['catid']);
	}
}
$oNews = new News();

if(!empty($_GET['btnExport'])){	// 导出
	$list = $oNews->get_list('*', $_GET, $orderby, 1, 65536, $offset = 0);
	if(!empty($list['list']) && is_array($list['list'])){
		$data = array();
		foreach($list['list'] as $row){
			$row['style'] = str_replace(',', ' ', $row['style']);
			$row['category_name'] = $newscats['category_list'][$row['catid']]['name'];
			$row['addtime'] = ftime($row['addtime']);
			$row['updatetime'] = ftime($row['updatetime']);
			$data[] = $row;
		}
		$headers = array(
			'id'			=> array('title' => 'ID',		'width' => '6'),
			'title'			=> array('title' => '标题',		'width' => '25',	'wrap' => true),
			'category_name'	=> array('title' => '分类', 	'width' => '12.5'),
			'summary'		=> array('title' => '摘要', 	'width' => '50',	'wrap' => true),
			'filename'		=> array('title' => '文件名', 	'width' => '12.5'),
			'rank'			=> array('title' => '排序', 	'width' => '8'),
			'hits'			=> array('title' => '点击量', 	'width' => '8'),
			'addtime'		=> array('title' => '添加时间', 'width' => '18'),
			'updatetime'	=> array('title' => '更新时间', 'width' => '18'),
		);
		if($list['total'] > 5000){	// 导出超过5000时用CSV
			export_csv($data, $headers, '_E_新闻_'.date('Ymd_His').'.csv');
		}else{
			$oExcel = new Excel();
			$oExcel->export($headers, $data, '_E_新闻_'.date('Ymd_His').'.xlsx');
		}
	}
	exit;
}

$list = $oNews->get_list('*', $_GET, $orderby, $__page, $__pagesize);
if(!empty($list['list'])){
	
	// 主键排序字段
	make_major_key_options(array(
		'id'	=> '添加时间',
		'catid'	=> '分类',
		'rank'	=> '排序',
		'hits'	=> '点击',
	));
	
	foreach($list['list'] as $key => $row){
		$row['style'] = str_replace(',', ' ', $row['style']);
		if(empty($row['url'])){
			if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
				if(empty($row['filename'])){
					$row['url'] = PUB_DIR.'news/'.time_to_path($row['addtime']).'/'.$row['id'].'.html';
				}else{
					$row['url'] = PUB_DIR.'news/v/'.$row['filename'].'.html';
				}
			}else{
				$row['url'] = WEB_DIR."index.php?mdl=news&do=view&id={$row['id']}";
			}
		}
		$row['category_name'] = $newscats['category_list'][$row['catid']]['name'];
		$row['addtime'] = ftime($row['addtime']);
		$row['updatetime'] = ftime($row['updatetime']);
		$list['list'][$key] = $row;
	}
	$vars['list'] = $list['list'];
	$vars['pagelist'] = $list['pagelist'];
}

$vars['catid'] = $_catid;

$oNewscat = new Newscat();
$vars['category_tree'] = $oNewscat->get_category_tree_ul();	// 取分类树;
if($_catid > 0){
	$vars['category_name'] = $newscats['category_list'][$_catid]['name'];
}else{
	$vars['category_name'] = $__l['root'];
}


?>